package com.leetcode.demo17;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Solution {

    /*public List<String> letterCombinations(String digits) {
        List<String> ans = new ArrayList<String>();
        for (int i = 0; i < digits.length(); i++) {
            ans = mul(ans, getList(digits.charAt(i) - '0'));//把数字转换为上面的字母  调用相乘，两两交叉相乘，类似于笛卡尔积
        }
        return ans;
    }

    public List<String> getList(int digit) {
        String digitLetter[] = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; //0 1按钮上面没有值
        List<String> ans = new ArrayList<String>();
        for (int i = 0; i < digitLetter[digit].length(); i++) {  //遍历取出一个按键上的几个字母
            ans.add(digitLetter[digit].charAt(i) + "");    // ans是由单个按键上的独立字母组成的list
        }
        return ans;
    }
    //定义成两个 List 相乘
    public List<String> mul(List<String> l1, List<String> l2) {
        if (l1.size() != 0 && l2.size() == 0) {
            return l1;
        }
        if (l1.size() == 0 && l2.size() != 0) {
            return l2;
        }
        List<String> ans = new ArrayList<String>();
        for (int i = 0; i < l1.size(); i++)        //两层for循环实现交叉相乘
            for (int j = 0; j < l2.size(); j++) {
                ans.add(l1.get(i) + l2.get(j));
            }
        return ans;
    }*/

    public List<String> letterCombinations(String digits) {
        LinkedList<String> ans = new LinkedList<String>();
        if(digits.isEmpty()) return ans;
        String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        ans.add("");
        for(int i =0; i<digits.length();i++){
            int x = Character.getNumericValue(digits.charAt(i));
            while(ans.peek().length()==i){ //查看队首元素
                String t = ans.remove(); //队首元素出队
                for(char s : mapping[x].toCharArray())
                    ans.add(t+s);
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        List<String> strings = solution.letterCombinations("234");
        System.out.println(strings);
    }
}
